﻿using CommonDevelop;
using CommonDevelop.DB;
using CommonFP;
using ManageDB;
using ManageDto;
using ManageRepository;
using System.Linq;
using System.Threading.Tasks;

namespace ManageService
{
    /// <summary>
    /// 机构服务层接口
    /// </summary>
    public interface ICompanyService : IBaseService<Base_Company>
    {
        Task<CompanyDto> GetDto(string CompanyName);
    }

    /// <summary>
    /// 机构服务实现
    /// </summary>
    public class CompanyService : BaseService<Base_Company, ICompanyRepository>, ICompanyService
    {
        /// <summary>
        /// 构造注入
        /// </summary>
        public CompanyService(ICompanyRepository repository)
            : base(repository)
        {

        }

        public override string BuildSeq()
            => SugarBizDB.BuildSeq_01("Com");

        public async Task<CompanyDto> GetDto(string CompanyName)
        {
            var pipe01 = FP.MakePipe<string, Base_Company>(n => this.GetEntity(a => a.FullName == n, false));

            var outcome =
                from CName in FP.Async(CompanyName.IsValidData())
                from acc in pipe01(CName)
                let dto = acc.MapTo<CompanyDto>()
                select dto;

            //var t = outcome.Recover(ex => FP.Error(ex.Message));
            var tt = outcome.Bind(a => FP.Async(a.Match(e => null, c => c)));
            return await tt;
        }
    }
}
